$(document).ready(function() {
	var mark = getQueryVariable('mark');
	//alert(mark);

	var myStorage = localStorage;
	//
	var hist = new Array();
	if(myStorage.getItem(mark) != null) {
		hist = JSON.parse(myStorage.getItem(mark));
	}
	//
	var histurl = new Array();
	if(myStorage.getItem("histurl") != null) {
		histurl = JSON.parse(myStorage.getItem("histurl"));
	}
	//
	var stars=new Array();
	if(myStorage.getItem("stars") != null) {
		stars = JSON.parse(myStorage.getItem("stars"));
	}
	//$("#ts").text(hist.toString());
	//
	var re = '';
	if(mark=='hist'){
		re=histurl;
		$("#ts").text("已看过"+histurl.length+"张图片.")
	}else if(mark=='star'){
		re=stars;
		$("#ts").text("已收藏"+re.length+"张图片.")
		$("#star").css("display","none");
	}
	else{
		$.getJSON("data/" + mark + ".json", function(result) {
			re = result;
		});	
	}

//	rrr=Math.round(Math.random() * re.length);
//	$("#gif2").attr("src", re[rrr]);
//

	//
	$("#btlogin").click(function() {
		//alert(myStorage.getItem('1')==null);
//		var nowdate = new Date().getTime()
//		var lastdate = new Date("2018-3-20 11:12:13").getTime()


//		if(nowdate > lastdate) {
//			$("#ts").text("响应国家净网行动，该软件暂时停止使用！请等待后续通知！");
//			$("#tuolikasi").attr("disabled","true");
//			return;
//		}
//		
//		if(localStorage.getItem("YES")==null){
//			if(histurl.length>10){
//				window.location.href="cz.html";
//			}
//		}


		$("#btlogin").text("正在加载..")
		$(this).css("background-color","#808080");
		$(this).attr("disabled", "true"); //设置变灰按钮  

		if($("#gif2").attr("alt")=="video"){
			$("#video").attr("src",$("#video2").attr("src"));
			$("#gif").css("display","none");
			$("#video").css("display","block");
		}else{
			$("#gif").attr("src",$("#gif2").attr("src"));
			$("#video").css("display","none");
			$("#gif").css("display","block");
		}
		
		var i = Math.round(Math.random() * (re.length-1));
		
//		while(($.inArray(i,hist)>-1)&&(mark!='hist')) {
//			i = Math.round(Math.random() * re.length);
//		}

		$("#gif2").attr("src", re[i][0]);
		$("#video2").attr("src",re[i][0]);
		$("#gif2").attr("alt", re[i][1]);
		//alert(re[i][1]);

		if((mark!='hist')&&(mark!='star')){
			hist.push(i);
			myStorage.setItem(mark, JSON.stringify(hist));
			histurl.push(re[i][0]);
			myStorage.setItem("histurl", JSON.stringify(histurl));
		}
		//alert(histurl)

	});
	
	//
	$("#star").click(function(){
		gif_stc=$("#gif").attr('src');
		if(($.inArray(gif_stc,stars)==-1)){
			stars.push(gif_stc);
		}
		myStorage.setItem("stars",JSON.stringify(stars));
		setTimeout("alert('已收藏')",1);


	});
	$("#tuolikasi").click(function(){
//		if(mark=='star'){
//			gif_stc=$("#gif").attr('src');
//			re.splice($.inArray(gif_stc,re),1);
//			myStorage.setItem("stars",JSON.stringify(re));
//			$("#ts").text("已收藏"+re.length+"张图片.")			
//		}
//		ii = Math.round(Math.random() * re.length);
//		$("#gif").attr("src",$("#gif2").attr("src"));
//		$("#gif2").attr("src", re[ii][0]);
//		
		$('#btlogin').css("background-color","#4CAF50");
		$('#btlogin').removeAttr('disabled');
		$("#btlogin").text("随机换一张");
	})




	document.getElementById("gif").onload = function() {
		$('#btlogin').css("background-color","#4CAF50");
		$('#btlogin').removeAttr('disabled');
		$("#btlogin").text("随机换一张");
	};
	document.getElementById("gif2").onload = function() {
		$('#btlogin').css("background-color","#4CAF50");
		$('#btlogin').removeAttr('disabled');
		$("#btlogin").text("随机换一张");
	};
	document.getElementById("video").onload = function() {
		$('#btlogin').css("background-color","#4CAF50");
		$('#btlogin').removeAttr('disabled');
		$("#btlogin").text("随机换一张");
	};
	document.getElementById("video2").onload = function() {
		$('#btlogin').css("background-color","#4CAF50");
		$('#btlogin').removeAttr('disabled');
		$("#btlogin").text("随机换一张");
	};



	function getQueryVariable(variable) {
		var query = window.location.search.substring(1);
		var vars = query.split("&");
		for(var i = 0; i < vars.length; i++) {
			var pair = vars[i].split("=");
			if(pair[0] == variable) {
				return pair[1];
			}
		}
		return(false);
	}

});